home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / modules / trackers / contact.js < prev    next >
Text File  |  2009-10-19  |  12KB  |  268 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* contact.js
  3.  * Responsible for tracking hContacts on pages
  4.  */
  5.  
  6. var EXPORTED_SYMBOLS = ["contact"];
  7. var contact = {
  8.     name: "Contact",
  9.     name_plural: "Contacts",
  10.     color: "#2F3A97",
  11.     isInternal: true,
  12.     _cache: null,
  13.     json: Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON),
  14.     onLoadTracks: function(url, doc){
  15.         var tracks = [];
  16.         this._cache = {};
  17.         if(doc){
  18.             var vcards = doc.getElementsByClassName('vcard');
  19.             var len = vcards.length;
  20.             for(var x = 0; x < len; x++){
  21.                 var node = vcards[x];
  22.                 var nnodes = node.getElementsByClassName('fn');
  23.                 if(nnodes.length){
  24.                     var name = nnodes[0].textContent;
  25.                     var tnodes = node.getElementsByClassName('tel');
  26.                     var anodes = node.getElementsByClassName('adr');
  27.                     var tlen = tnodes.length;
  28.                     var alen = anodes.length;
  29.                     if(tlen || alen){
  30.                         var info = { name: name, phone: [] };
  31.                         if(alen){
  32.                             var addr = anodes[0];
  33.                             var list = addr.getElementsByClassName('locality');
  34.                             if(list.length){
  35.                                 info['city'] = list[0].textContent;
  36.                                 list = addr.getElementsByClassName('street-address');
  37.                                 if(list.length){
  38.                                     info['address'] = list[0].textContent;
  39.                                 }
  40.                                 list = addr.getElementsByClassName('region');
  41.                                 if(list.length){
  42.                                     info['state'] = list[0].textContent;
  43.                                 }
  44.                                 list = addr.getElementsByClassName('postal-code');
  45.                                 if(list.length){
  46.                                     info['zip'] = list[0].textContent;
  47.                                 }
  48.                                 list = addr.getElementsByClassName('country-name');
  49.                                 if(list.length){
  50.                                     info['country'] = list[0].textContent;
  51.                                 }
  52.                             // check for malformed google maps results
  53.                             } else if(addr.id && addr.id.match(/^panel/)){
  54.                                 list = addr.getElementsByClassName('name');
  55.                                 if(list.length){
  56.                                     var namefields = list[0].getElementsByClassName('gcpaddrlink');
  57.                                     if(namefields.length){
  58.                                         name = namefields[0].textContent;
  59.                                         info.name = this.app.getString('contact.noname',name);
  60.                                         info.address = name;
  61.                                     }
  62.                                 }
  63.                                 list = addr.getElementsByClassName('atxt');
  64.                                 if(list.length){
  65.                                     info['city'] = list[0].textContent;
  66.                                 }
  67.                             }
  68.                         }
  69.                         for(var y = 0; y < tlen; y++){
  70.                             var telephone = tnodes[y].textContent;
  71.                             if(telephone.length){
  72.                                 info.phone.push(telephone);
  73.                             }
  74.                         }
  75.                         if(info.city || info.phone.length){
  76.                             var preview = this.json.encode(info);
  77.                             if(!this._cache[preview]){
  78.                                 tracks.push({
  79.                                     type: "contact",
  80.                                     title: info.name,
  81.                                     preview: preview,
  82.                                     content: name
  83.                                     }
  84.                                 );
  85.                                 this._cache[preview] = true;
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         return tracks;
  93.     },
  94.     onUnloadTracks: function(url, doc){
  95.         var tracks = [];
  96.         if(doc){
  97.             var vcards = doc.getElementsByClassName('vcard');
  98.             var len = vcards.length;
  99.             for(var x = 0; x < len; x++){
  100.                 var node = vcards[x];
  101.                 var nnodes = node.getElementsByClassName('fn');
  102.                 if(nnodes.length){
  103.                     var name = nnodes[0].textContent;
  104.                     var tnodes = node.getElementsByClassName('tel');
  105.                     var anodes = node.getElementsByClassName('adr');
  106.                     var tlen = tnodes.length;
  107.                     var alen = anodes.length;
  108.                     if(tlen || alen){
  109.                         var info = { name: name, phone: [] };
  110.                         if(alen){
  111.                             var addr = anodes[0];
  112.                             var list = addr.getElementsByClassName('locality');
  113.                             if(list.length){
  114.                                 info['city'] = list[0].textContent;
  115.  
  116.                                 list = addr.getElementsByClassName('street-address');
  117.                                 if(list.length){
  118.                                     info['address'] = list[0].textContent;
  119.                                 }
  120.                                 list = addr.getElementsByClassName('region');
  121.                                 if(list.length){
  122.                                     info['state'] = list[0].textContent;
  123.                                 }
  124.                                 list = addr.getElementsByClassName('postal-code');
  125.                                 if(list.length){
  126.                                     info['zip'] = list[0].textContent;
  127.                                 }
  128.                                 list = addr.getElementsByClassName('country-name');
  129.                                 if(list.length){
  130.                                     info['country'] = list[0].textContent;
  131.                                 }
  132.                             // check for malformed google maps results
  133.                             } else if(addr.id && addr.id.match(/^panel/)){
  134.                                 list = addr.getElementsByClassName('name');
  135.                                 if(list.length){
  136.                                     var namefields = list[0].getElementsByClassName('gcpaddrlink');
  137.                                     if(namefields.length){
  138.                                         name = namefields[0].textContent;
  139.                                         info.name = this.app.getString('contact.noname',name);
  140.                                         info.address = name;
  141.                                     }
  142.                                 }
  143.                                 list = addr.getElementsByClassName('atxt');
  144.                                 if(list.length){
  145.                                     info['city'] = list[0].textContent;
  146.                                 }
  147.                             }
  148.                         }
  149.                         for(var y = 0; y < tlen; y++){
  150.                             var telephone = tnodes[y].textContent;
  151.                             if(telephone.length){
  152.                                 info.phone.push(telephone);
  153.                             }
  154.                         }
  155.                         if(info.city || info.phone.length){
  156.                             var preview = this.json.encode(info);
  157.                             if(!this._cache[preview]){
  158.                                 tracks.push({
  159.                                     type: "contact",
  160.                                     title: info.name,
  161.                                     preview: preview,
  162.                                     content: name
  163.                                     }
  164.                                 );
  165.                                 this._cache[preview] = true;
  166.                             }
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.         this._cache = {};
  173.         return tracks;
  174.     },
  175.     preview: function(doc, node, data){
  176.         var item = this.json.decode(data.preview);
  177.         var hbox, label, spacer;
  178.         // map
  179.         if(item.city){
  180.             var iframe = doc.createElement(
  181.                 "iframe"
  182.             );
  183.             var addr = item.address || ' ';
  184.             var st = item.state || ' ';
  185.             var zip = item.zip || ' ';
  186.             var cty = item.country || ' ';
  187.             iframe.setAttribute("src", 
  188.                     "http://ext.boomtango.com/map.html?address=" +
  189.                     encodeURIComponent(this.app.getString(
  190.                         "contact.mapformat",addr,item.city,st, zip, cty)));
  191.             iframe.setAttribute("width", "352");
  192.             iframe.setAttribute("height", "296");
  193.             node.appendChild(iframe);
  194.         }
  195.         // name
  196.         hbox = doc.createElement('hbox');
  197.         hbox.setAttribute('style', 'padding-left: 8px;');
  198.         label = doc.createElement('label');
  199.         label.setAttribute('value', item.name);
  200.         label.setAttribute('flex', '1');
  201.         label.setAttribute('crop', 'end');
  202.         hbox.appendChild(label);
  203.         spacer = doc.createElement('spacer');
  204.         spacer.setAttribute('flex', '100');
  205.         hbox.appendChild(spacer);
  206.         node.appendChild(hbox);
  207.         
  208.         // address
  209.         if(item.address){
  210.             hbox = doc.createElement('hbox');
  211.             hbox.setAttribute('style', 'padding-left: 8px;');
  212.             label = doc.createElement('label');
  213.             label.setAttribute('value', item.address);
  214.             label.setAttribute('flex', '1');
  215.             label.setAttribute('crop', 'end');
  216.             hbox.appendChild(label);
  217.             spacer = doc.createElement('spacer');
  218.             spacer.setAttribute('flex', '100');
  219.             hbox.appendChild(spacer);
  220.             node.appendChild(hbox);
  221.         }
  222.         // city, st, zip
  223.         if(item.city){
  224.             hbox = doc.createElement('hbox');
  225.             hbox.setAttribute('style', 'padding-left: 8px;');
  226.             label = doc.createElement('label');
  227.             var st = item.state || ' ';
  228.             var zip = item.zip || ' ';
  229.             label.setAttribute('value', 
  230.                     this.app.getString("contact.citystatezip",
  231.                         item.city, st, zip));
  232.             label.setAttribute('flex', '1');
  233.             label.setAttribute('crop', 'end');
  234.             hbox.appendChild(label);
  235.             spacer = doc.createElement('spacer');
  236.             spacer.setAttribute('flex', '100');
  237.             hbox.appendChild(spacer);
  238.             node.appendChild(hbox);
  239.         }
  240.         // country
  241.         if(item.country){
  242.             hbox = doc.createElement('hbox');
  243.             label = doc.createElement('label');
  244.             label.setAttribute('value', item.country);
  245.             label.setAttribute('flex', '1');
  246.             label.setAttribute('crop', 'end');
  247.             hbox.appendChild(label);
  248.             spacer = doc.createElement('spacer');
  249.             spacer.setAttribute('flex', '100');
  250.             hbox.appendChild(spacer);
  251.             node.appendChild(hbox);
  252.         }
  253.  
  254.         for(var x = 0; x < item.phone.length; x++){
  255.             hbox = doc.createElement('hbox');
  256.             label = doc.createElement('label');
  257.             label.setAttribute('value', item.phone[x]);
  258.             label.setAttribute('flex', '1');
  259.             label.setAttribute('crop', 'end');
  260.             hbox.appendChild(label);
  261.             spacer = doc.createElement('spacer');
  262.             spacer.setAttribute('flex', '100');
  263.             hbox.appendChild(spacer);
  264.             node.appendChild(hbox);
  265.         }
  266.     }
  267. };
  268.